package com.atguigu.spzx.manager.service.impl;

import com.atguigu.spzx.manager.mapper.CategoryMapperZxw;
import com.atguigu.spzx.manager.service.CategoryServiceZxw;
import com.atguigu.spzx.model.entity.product.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.List;

@Service
public class CategoryServiceImplZxw implements CategoryServiceZxw {
    @Autowired
    private CategoryMapperZxw categoryMapperZxw;


    @Override
    public List<Category> findCategoryList(Long id) {
        // 根据分类id查询它下面的所有的子分类数据
        List<Category> categoryList = categoryMapperZxw.selectCategoryByParentId(id);
        //通过这个方法-去判断这个集合不等于空
        if (!CollectionUtils.isEmpty(categoryList)) {
            categoryList.forEach(item -> {
                // 查询该分类下子分类的数量
                Integer count = categoryMapperZxw.selectCountByParentId(id);
                if (count > 0) {
                    item.setHasChildren(true);
                } else {
                    item.setHasChildren(false);
                }
            });
        }
        return categoryList;
    }
}
